package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态展示菜单
 * 根据当前登录用户查询 t_menu 表中的数据, 动态展示菜单数据
 *
 * @王鹏
 */

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

	//动态菜单展示
	@RequestMapping("/findMenuByUsername")
	public Result findMenuByUsername(String username) {
		//调用service查询t_menu表,将数据封装到List集合中,回显数据
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		try {
			//根据用户名查询菜单表
			List<Menu> menuList = menuService.findMenuByUsername(name);
			return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_MENU_FAIL);
		}
	}

    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.findAll(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,String> map) {
        try {
            menuService.add(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_SUCCESS);
        }
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findByMenuId")
    public Result findByMenuId(Integer id){
        try {
            Map<String, Object> menu = menuService.findById(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,String> map){
        try {
            menuService.edit(map);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);

        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findParentmenu")
    public Result findParentmenu(){
        try {
            List<Menu> parentmenu = menuService.findParentmenu();
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,parentmenu);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }
}
